package llama.view;

import java.awt.BorderLayout;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import llama.core.IUser;
import llama.core.Message;
import llama.network.NetworkMessage;
import llama.util.SystemMessage;
import llama.view.panels.ChatInputPanel;
import llama.view.panels.ChatTabPanel;


public class ChatFrame extends JFrame implements PropertyChangeListener {

	private JPanel contentPane;
	private ChatTabPanel chatTabPanel; 
	
	private ChatInputPanel chatInputPanel;
	

	/**
	 * Create the frame.
	 */
	public ChatFrame() {
		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		chatTabPanel = new ChatTabPanel();
		chatInputPanel = new ChatInputPanel();
		
		contentPane.add(chatTabPanel, BorderLayout.CENTER);
		contentPane.add(chatInputPanel, BorderLayout.SOUTH);
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				chatTabPanel.clearTabs();
			}
			
		});
		
		this.addWindowFocusListener(new WindowAdapter() {
			@Override
			public void windowGainedFocus(WindowEvent e) {
				super.windowGainedFocus(e);
				chatInputPanel.setFocusToInput();
			}
		});
		
		setVisible(false);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if((evt.getNewValue()!=null) && evt.getNewValue() instanceof NetworkMessage) {
			NetworkMessage nMsg = (NetworkMessage) evt.getNewValue();
			switch (nMsg.getType()) {
			case LOGIN_SUCCESS:
				break;
			case LOGIN_FAIL:
				break;
			case MESSAGE:
				setVisible(true);
				Message msg = ((Message) nMsg.getObject());
				chatTabPanel.appendMessage(msg);
				break;
			case DISCONNECTED:
				chatTabPanel.clearTabs();
				setVisible(false);
				break;
			}
		} else if((evt.getNewValue()!=null) && evt.getNewValue() instanceof SystemMessage) {
			SystemMessage sMsg = (SystemMessage) evt.getNewValue();
			switch (sMsg.getType()) {
			case NETWORK_UNAVAILABLE:
				break;
			case SHOW_SEARCH_WINDOW:
				break;
			case SELECT_CONVERSATION:
				setVisible(true);
				IUser user = (IUser) sMsg.getObject();
				chatInputPanel.setReceiver(user);
				setTitle(user.getEmail());
				chatTabPanel.activateConversation(user);
				break;
			case CLOSE_CONVERSATION:
				closeConversation((IUser) sMsg.getObject());
				break;
			case LOGOUT:
				chatTabPanel.clearTabs();
				setVisible(false);
				break;
			}
		}
	}
	
	private void closeConversation(final IUser email) {
		if(chatTabPanel.getTabCount() > 1) {
			chatTabPanel.closeTab(email);
		} else {
			chatTabPanel.clearTabs();
			this.setVisible(false);
		}
		
	}
}
